package com.gimi.cloud.bbp.manage.controller.mall;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.enums.ServiceNameConstants;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.mall.MallAppRecommentDTO;
import com.gimi.cloud.bbp.dto.mall.MallAppRecommentGroupDTO;
import com.gimi.cloud.bbp.manage.service.mall.MallAppRecommentGroupService;
import com.gimi.cloud.bbp.manage.service.mall.MallAppRecommentService;
import com.github.pagehelper.PageInfo;

/**
 * t推送信息组相关controller
 * @author chendi
 * @date 2018年2月1日
 */
@RestController
@RequestMapping("/mall/recommentGroup")
public class MallRecommentGroupController {
    private static final Logger logger = LoggerFactory.getLogger(MallRecommentGroupController.class);

    private MallAppRecommentGroupService mallAppRecommentGroupService;
    private MallAppRecommentService mallAppRecommentService;

    @Autowired
    public MallRecommentGroupController(MallAppRecommentGroupService mallAppRecommentGroupService, MallAppRecommentService mallAppRecommentService) {
        this.mallAppRecommentGroupService = mallAppRecommentGroupService;
        this.mallAppRecommentService = mallAppRecommentService;
    }

    /**
     * 获取推送组列表
     * @param request
     * @param response
     * @param dto 查询参数
     * @throws Exception
     */
    @RequestMapping("/list")
    public Response list(HttpServletRequest request, HttpServletResponse response,@RequestBody MallAppRecommentGroupDTO dto) throws Exception{
        Response resp = new Response();
        List<MallAppRecommentGroupDTO> recommentGroups = mallAppRecommentGroupService.queryList(dto,true);
        PageInfo<MallAppRecommentGroupDTO> pageInfo = new PageInfo<>(recommentGroups);
        Map<String,Object> resData = new HashMap<>();
        resData.put(ServiceNameConstants.PAGEDATA_KEY,pageInfo);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    /**
     * 获取单一的RecommentGroup信息
     * @param request
     * @param response
     * @param dto
     * @throws Exception
     */
    @RequestMapping("/info")
    public Response info(HttpServletRequest request,HttpServletResponse response,@RequestBody MallAppRecommentGroupDTO dto) throws Exception{
        Response resp = new Response();
        MallAppRecommentGroupDTO appRecommentGroup = mallAppRecommentGroupService.load(dto,dto.getArgId());
        if(null != appRecommentGroup){
            MallAppRecommentDTO mallRecomment = new MallAppRecommentDTO();
            mallRecomment.setArgId(dto.getArgId());
            List<MallAppRecommentDTO> recommentList = mallAppRecommentService.queryList(mallRecomment,false);
            appRecommentGroup.setMallAppRecomments(recommentList);
        }
        Map<String,Object> resData = new HashMap<>();
        resData.put("appRecommentGroup",appRecommentGroup);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"获取成功");
        return resp;
    }

    /**
     * 新增或编辑推送组信息
     * @param request
     * @param response
     * @param dto 待保存的数据
     * @throws Exception
     */
    @RequestMapping("/saveOrUpdate")
    public Response saveOrUpdate(HttpServletRequest request,HttpServletResponse response,@RequestBody MallAppRecommentGroupDTO dto) throws Exception{
        Response resp = new Response();
        mallAppRecommentGroupService.saveOrUpdate(dto);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"保存成功");
        return resp;
    }
    /**
     * 删除推送组数据
     * @param request
     * @param response
     * @param dto 待删除的banner组数据
     * @throws Exception
     */
    @RequestMapping("/delete")
    private Response delete(HttpServletRequest request,HttpServletResponse response,@RequestBody MallAppRecommentGroupDTO dto) throws  Exception{
        Response resp = new Response();
        mallAppRecommentGroupService.delete(dto,dto.getArgId(),false);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"删除成功");
        return resp;
    }


}
